package com.hangzhou.xyj.ui.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.ActionModel;
import com.hangzhou.xyj.model.ActionPageInfoModel;
import com.hangzhou.xyj.net.handler.GetListOfInfoHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetListOfInfoReq;
import com.hangzhou.xyj.ui.adapter.FindActionDetailAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.fragment.base.BaseFragment;
import com.hangzhou.xyj.ui.widget.DividerItemDecoration;
import com.hangzhou.xyj.utils.constants.Constants;

import java.util.List;

/**
 * 发现->活动
 * <p/>
 * Created by Administrator on 2016/1/15.
 */
public class FindActionItemFragment extends BaseFragment implements View.OnClickListener {

    public static final int SCOPE_TYPE_COUNTRY = 1;//全国
    public static final int SCOPE_TYPE_CITY = 2;//同城
    public static final int SCOPE_TYPE_FRIENDS = 3;//票友
    private int mScopeType;
    private RecyclerView mRecyclerView;
    private FindActionDetailAdapter mAdapter;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void initDatas() {
        getActionList(mScopeType, 1);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.fragment_find_action_content);
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getActionList(mScopeType, mAdapter.getCurrentPage());
                    }
                }, 800);
            }
        });
    }

    @Override
    public void initTop() {
        enableTop(false);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_content);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new FindActionDetailAdapter(mRecyclerView, null));
        //设置水平分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
                , R.drawable.line_divider));
        mScopeType = getArguments().getInt("scopeType");//获得传递过来的scopeType
        enableRefresh(true);
    }

    /**
     * 获取活动列表
     *
     * @param scopeType
     * @param currentPage
     */
    public void getActionList(int scopeType, final int currentPage) {
        GetListOfInfoReq getListOfInfoReq;
        switch (scopeType) {
            case SCOPE_TYPE_COUNTRY:
                getListOfInfoReq = new GetListOfInfoReq(1, SCOPE_TYPE_COUNTRY, currentPage, Constants.PAGE_SIZE);
                break;
            case SCOPE_TYPE_CITY:
                getListOfInfoReq = new GetListOfInfoReq(1, SCOPE_TYPE_CITY, currentPage, Constants.PAGE_SIZE);
                break;
            case SCOPE_TYPE_FRIENDS:
            default:
                getListOfInfoReq = new GetListOfInfoReq(1, SCOPE_TYPE_FRIENDS, currentPage, Constants.PAGE_SIZE);
                break;
        }
        HttpLoader.getInstance().sendRequest(new GetListOfInfoHandler(getListOfInfoReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<ActionModel> tempList = ((ActionPageInfoModel) model).getActionList();//获取的数据
                if (currentPage == 1) {
                    mAdapter.load(tempList);
                } else {
                    mAdapter.update(tempList);
                }
            }

            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }
}
